package cn.hughie.thread;

/**
 * 线程中断适用于取消某个异步操作的情况
 * 只能由其他线程调用也可以自己调用 并且线程中断只是为了解决等待或者阻塞  而不是用于正常线程的中断
 * 如果用于正产线程的中断 那么需要加上一步判断
 * 中断只表示在当前阻塞状态退出
 * @Author yuh
 * @Date Created in 上午7:50 2018/5/24
 * @Description
 */
public class Thread3 extends Thread {
    @Override
    public void run() {


        System.out.println("Going...");
        if (Thread.interrupted()) {
            System.out.println("Someone interrupted me.");
        }
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        System.out.println("Going...");
        long now = System.currentTimeMillis();
        while (System.currentTimeMillis() - now < 1000) {
            // 为了避免Thread.sleep()而需要捕获InterruptedException而带来的理解上的困惑,
            // 此处用这种方法空转1秒
        }
    }


    public static void main(String[] args) throws InterruptedException {
        Thread3 t = new Thread3();
        t.start();
        //Thread.sleep(3000);
        t.interrupt();
    }
}
